#!/usr/bin/env python
#-*-coding:utf-8-*-
import  re
from  urllib import  request,parse
from  http import cookiejar
import json
from  time import  sleep
import getproxy
urll='http://39.108.226.55/api/profile'
urlll='http://39.108.226.55/api/submission'
url='http://39.108.226.55/api/login'
filename='cooke.txt'

cooke=cookiejar.CookieJar() #下面为其子类实现保存到文件
head = {}
head['User-Agent']=r'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'
head['Content-Type']='application/json;'
#head['Cookie']='csrftoken=ltrpwtUsJkTal4b3cmnpvWQXw0L9NvG2EXix0qLYLpvbPM7NAxEFYZiX9nfpuY11'
te=request.HTTPCookieProcessor(cooke)
openner=request.build_opener(te)
res=request.Request(url=urll,headers=head)
tf=openner.open(res)
print(tf.read())
s1=""
s2=""
for i in cooke:
    s1=i.name
    s2=i.value

head['X-CSRFToken']=s2

date={}
te=request.HTTPCookieProcessor(cooke)
openner=request.build_opener(te)
#data=parse.urlencode(date).encode('utf-8')
date={"username":"a123456"}
date["password"]="a123456"
data=json.JSONEncoder().encode(date).encode('utf-8')
req=request.Request(url=url,headers=head,data=data)
thml=openner.open(req).read()
print(thml.decode('utf-8'))
dic={}
for item in cooke:
    head['X-CSRFToken']=item.value
    break
te=request.HTTPCookieProcessor(cooke)
openner=request.build_opener(te)
proxy=request.ProxyHandler({'http':'125.46.0.62:53281'})
openner1=request.build_opener(proxy,te)
i=1
sss=""
with open('1') as f:
    sss=f.read()
rge='.'
global null
null=None
while i<236:
    date = {"problem_id": i, "language": "C++", "code": sss}
    data = json.JSONEncoder().encode(date).encode('utf-8')
    req = request.Request(url=urlll, headers=head, data=data)
    if(i%2==3):
     html = openner.open(req,timeout=3)
     print(html.read())
    else:
       try:
           html = openner1.open(req, timeout=2)
           q=html.read().decode('utf-8')
           print(q)
           dic=eval(q)
           if(not dic['error']):
             i=i+1
           else:
             ss=re.findall('[0-9]+',dic['data'])
             print(int(ss[0]))
             sleep(int(ss[0])+0.5)
             #i=i+1
       except:
           print('sss')
           s=getproxy.get_()
           print('ip异常 新ip为  '+s)
           proxy = request.ProxyHandler({'http': s})
           openner1 = request.build_opener(proxy, te)

    print(i)
#print(html.read().decode())












